package jackson.src.day03;

/*
 * Author: JacksonCoder
 * FileName: StringBuilderDemo
 * Version: 1.0.0
 */
public class StringBuilderDemo {
    public static void main(String[] args) {
//        String s = "";
//        for(int i = 0; i < 1000 ; i++){
//            s = s + "," + i;
//            System.out.println(s);
//        }
        StringBuilder sb = new StringBuilder(1024);
        sb.append("Mr ").append("Jackson").append("!").insert(0, "Hello, ");
        System.out.println(sb.toString());

        Addr addr = new Addr();
        addr.add(1).add(2).add(100).inc().add(10);
        System.out.println(addr.value());

        String[] fields = {"name", "position", "salary"};
        String table = "employee";
        String insert = buildInsertSql(table, fields);
        System.out.println(insert);
        String s = "INSERT INTO employee (name, position, salary) VALUES (?, ?, ?)";
        System.out.println(s.equals(insert) ? "测试成功" : "测试失败");
    }

    static String buildInsertSql(String table, String[] fields) {
        String result = "INSERT INTO ";
        String content = String.join(", ", fields);
        StringBuilder strBud =  new StringBuilder(1024);
        strBud.append(result).append(table).append(" (").append(content).append(")").append(" VALUES (?, ?, ?)");
        return strBud.toString();
    }

}

class Addr {
    private int sum = 0;

    public Addr add(int n) {
        sum += n;
        return this;
    }

    public Addr inc() {
        sum++;
        return this;
    }

    public int value() {
        return sum;
    }

}
